<%

/*
 * Copyright 2006 Brian Watkins
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

%>

<%@ page import="java.util.*" %>
<%@ page import="bw.news.*" %>
<%@ page import="bw.util.Log" %>
<%@ page import="bw.util.RefresherException" %>

<%

  NewsManager manager = NewsManager.getInstance();
  
  int categoryId = Integer.parseInt( request.getParameter( "catId" ) );
  String action = request.getParameter( "action" );

  NewsCategory category = manager.getNewsCategoryForId( categoryId );

  String addedFeedId = request.getParameter( "addedFeed" );
  NewsSource addedSource = null;
  if ( addedFeedId != null ) { 
    addedSource = manager.getNewsSourceForId( addedFeedId ); 
  }

  String deletedFeedId = null;
  if ( "delete".equals( action ) ) {
    String feedId = request.getParameter( "feedId" );
    NewsSource source = category.getNewsSourceForId( feedId );
    category.deleteNewsSource( source );
    manager.syncChanges();
    deletedFeedId = feedId;
  }

  String message = null;
  if ( "refresh".equals( action ) ) {
    String feedId = request.getParameter( "feedId" );
    NewsSource source = category.getNewsSourceForId( feedId );
    try {
      source.updateSource();
      message = source.getName() + " successfully updated!";
    } catch ( RefresherException ex ) {
      Log.getInstance().write( "Problem manually refreshing: " + source.getSrc(), ex );
      message = "Problem updating source: " + source.getName();
    }
  }

  String editedFeedId = request.getParameter( "editedFeed" );
  NewsSource editedSource = null;
  if ( editedFeedId != null ) {
    //use the manager method here because the edited feed may
    //have been transfered to another category
    editedSource = manager.getNewsSourceForId( editedFeedId );
  }

  String movedToCategory = request.getParameter( "movedToCat" );

%>
<html>
<head>
<link rel=STYLESHEET type="text/css" href="style.jsp">
</head>
<body bgcolor="#003366" style="margin:0px;">

<div class="editItems">

<% if ( message != null ) { %>
<div class="noNewsItem">
<b><%= message %></b>
</div>
<% } %>

<%
  Vector sources = category.getNewsSources();
  for ( Enumeration e = sources.elements(); e.hasMoreElements(); ) {
    NewsSource source = (NewsSource) e.nextElement();
%>
<div id="editItem_<%= source.getId() %>" class="editItem">
<div class="editItemTitle">
<p class="editItemCategory"><%= category.getName() %></p>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td class="editItemSource"><%= source.getName() %></td>
<td align="right" valign="bottom" class="eiAction"><a href="editNewsCategory.jsp?action=refresh&catId=<%= category.getId() %>&feedId=<%= source.getId() %>" class="editItemActionLink">Refresh</a></td>
</tr>
</table>
</div>
<div class="editItemSummary">
Refreshing every <%= source.getRefreshStr() %> minutes.
<div class="editItemSummaryLinks">Source: <a href="<%= source.getSrc() %>" target=_new><%= source.getSrc() %></a>
<br>Site: <a href="<%= source.getSite() %>" target=_new><%= source.getSite() %></a>
</div>
<% if ( source.getPassword() != null && source.getUsername() != null ) { %>
<div class="eiPass">
Password protected for username: <%= source.getUsername() %>
</div>
<% } %>
</div>
<div class="editItemActions">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td class="eiAction"><a href="addNews.jsp?catId=<%= category.getId() %>&feedId=<%= source.getId() %>" class="editItemActionLink">Edit</a></td><td align="right" class="eiAction"><a href="editNewsCategory.jsp?action=delete&catId=<%= category.getId() %>&feedId=<%= source.getId() %>" class="editItemActionLink">Delete</a></td>
</tr></table>
</div>
</div>

<% } %>

</div>

</body>

<script>

<% if ( addedSource != null ) { %>

window.top.addNewsFeed( '<%= category.getId() %>', '<%= addedSource.getName() %>', '<%= addedSource.getId() %>' );

<% } %>

<% if ( deletedFeedId != null ) { %>

window.top.deleteNewsFeed( '<%= category.getId() %>', '<%= deletedFeedId %>' );

<% } %>

<% if ( editedSource != null ) { 
     if ( movedToCategory == null ) { 
%>

window.top.editNewsFeed( '<%= category.getId() %>', '<%= editedSource.getName() %>', '<%= editedSource.getId() %>' );

<%   } else { %>

window.top.deleteNewsFeed( '<%= category.getId() %>', '<%= editedSource.getId() %>' );
window.top.addNewsFeed( '<%= movedToCategory %>', '<%= editedSource.getName() %>', '<%= editedSource.getId() %>' );

<%   }
   } 
%>

window.top.document.getElementById( "newsFeedWindow" ).focus();

</script>

</html>